﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Creator.Core.DataBind
{
    /// <summary>
    /// 绑定配置
    /// 标识一个类型中属性，应该与界面做什么样的绑定
    /// </summary>
    public class BindConfig
    {
        public List<BindProperty> Binds { get; set; } = new List<BindProperty>();

        /// <summary>
        /// 目标类型
        /// </summary>
        public Type TargetType { get; set; }


        public bool AddBind(string propertyName)
        {
            if (Binds.Where(a => a.PropertyName == propertyName).Count() > 0)
            {
                return false;
            }
            var prop = TargetType.GetProperty(propertyName);
            Binds.Add(new BindProperty(prop.Name, prop.PropertyType));
            return true;
        }

        public bool RemoveBind(string propertyName)
        {
            if (Binds.Where(a => a.PropertyName == propertyName).Count() == 0)
            {
                return false;
            }
            Binds.Remove(Binds.Where(a => a.PropertyName == propertyName).First());
            return true;
        }

    }

    public class BindProperty
    {
        public BindProperty(string propertyName, Type propertyType)
        {
            PropertyName = propertyName;
            PropertyType = propertyType;
        }

        private Type propertyType;
        /// <summary>
        /// 属性名称
        /// </summary>
        public string PropertyName { get; }
        
        /// <summary>
        /// 绑定的属性
        /// </summary>
        public Type PropertyType { get; }

    }
}
